package com.avicsafety.lib.baidumap;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.avicsafety.lib.baidumap.R;
import com.baidu.mapapi.search.core.PoiInfo;

import java.util.List;

/**
 * Created by Administrator on 2017/6/15.
 * 为位置列表添加适配器
 */

public class PlaceListAdapter extends BaseAdapter {

    public LayoutInflater mInflater;
    public List<PoiInfo> mList;
    public int notifyTip;

    public PlaceListAdapter(LayoutInflater mInflater, List<PoiInfo> mList) {
        this.mInflater = mInflater;
        this.mList = mList;
        this.notifyTip = -1;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        MyViewHolder myViewHolder;
        if (view==null){
            view=mInflater.inflate(R.layout.listitem_place,viewGroup,false);
            myViewHolder=new MyViewHolder();
            myViewHolder.placeName= (TextView) view.findViewById(R.id.place_name);
            myViewHolder.placeAddree= (TextView) view.findViewById(R.id.place_adress);
            myViewHolder.placeSelected= (ImageView) view.findViewById(R.id.place_select);
            myViewHolder.placeName.setText(mList.get(i).name);
            myViewHolder.placeAddree.setText(mList.get(i).address);
            myViewHolder.placeSelected.setBackgroundResource(R.drawable.greywhite);
            view.setTag(myViewHolder);
        }else {
            myViewHolder= (MyViewHolder) view.getTag();
        }

        //TODO
        myViewHolder.placeName.setText(mList.get(i).name);
        myViewHolder.placeAddree.setText(mList.get(i).address);
        //根据从新加载的时候第i条item是否是当前所选择的条目,选择加载不同的图片
        if (notifyTip==i){
            myViewHolder.placeSelected.setBackgroundResource(R.drawable.ic_select);
        }else {
            myViewHolder.placeSelected.setBackgroundResource(R.drawable.greywhite);
        }
        return view;
    }

    /**
     * 设置第几个item被选中
     */
    public void setNotifyTip(int notifyTip) {
        this.notifyTip = notifyTip;
    }

    public int getNotifyTip() {
        return notifyTip;
    }

    private class MyViewHolder{
        TextView placeAddree;
        TextView placeName;
        ImageView placeSelected;
    }

}
